# QTableWidget 控件使用
from PyQt5.QtWidgets import QTableView, QAbstractItemView, QHeaderView, QTableWidget, QTableWidgetItem, QMessageBox, \
    QListWidget, QListWidgetItem, QStatusBar, QMenuBar, QMenu, QAction, QLineEdit, QStyle, QFormLayout, QVBoxLayout, \
    QWidget, QApplication, QHBoxLayout, QPushButton, QMainWindow, QGridLayout, QLabel
from PyQt5.QtGui import QIcon, QPixmap, QStandardItem, QStandardItemModel, QCursor
from PyQt5.QtCore import QStringListModel, QAbstractListModel, QModelIndex, QSize, Qt
import sys


class WindowClass(QWidget):

    def __init__(self, parent=None):

        super(WindowClass, self).__init__(parent)
        self.layout = QHBoxLayout()
        self.resize(400, 300)
        tableWidget = QTableWidget()
        tableWidget.setRowCount(4)  # 行数
        tableWidget.setColumnCount(4)  # 列数
        tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)  # 所有列自动拉伸，充满界面
        tableWidget.setSelectionMode(QAbstractItemView.SingleSelection)  # 设置只能选中一行
        tableWidget.setEditTriggers(QTableView.NoEditTriggers)  # 不可编辑
        tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)  # 设置只有行选中

        self.layout.addWidget(tableWidget)
        tableWidget.setHorizontalHeaderLabels(['姓名', '地址', '年龄', '工资'])  # 横向标题排列，如果使用setVerticalHeaderLabels则是纵向排列标题
        items = [['JONES', 'Beijing', '23', 2300], ['SMITH', 'ShangHai', '23', '3000'], ['ZY', 'Tianjing', '23', '2000'],
                 ['Smith', 'SJT', '22', '1030']]
        for i in range(len(items)):  # 注意上面列表中数字加单引号，否则下面不显示(或者下面str方法转化一下即可)
            item = items[i]
            for j in range(len(item)):
                item = QTableWidgetItem(str(items[i][j]))
                tableWidget.setItem(i, j, item)
        self.setLayout(self.layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = WindowClass()
    win.show()
    sys.exit(app.exec_())
